<template>
	<view>
		<back :showBackText="false" customClass="bg-gradual-blue text-white" title="在线网课"></back>
		<view class="flex flex-direction" style="min-height: 100vh;" v-if="isLoad">
			<!-- 固定顶部搜索栏 -->
			<view class="cu-bar bg-white search ">
				<view class="search-form round">
					<text class="cuIcon-search"></text>
					<input type="text" v-model="keyword" @input="onInputChange" @confirm="searchTap"
						placeholder="搜索关键字" confirm-type="search"></input>
				</view>
				<view class="action" @tap="searchTap">
					<text class="text-blue">搜索</text>
				</view>
			</view>

			<!-- 视频列表 -->
			<view class="video-list padding-lr" style="margin-bottom: 120rpx;">
				<view class="video-card" v-for="(item, index) in videoList" :key="index" @tap="videoTap"
					:data-index="index">
					<view class="video-cover">
						<image :src="item.cover" mode="aspectFill" lazy-load></image>
						<view class="video-duration" v-if="item.video_num">
							<text>共{{item.video_num}}课时</text>
						</view>
						<view class="video-tag" v-if="item.is_hot">
							<text>HOT</text>
						</view>
						<view class="play-icon">
							<text class="cuIcon-playfill"></text>
						</view>
					</view>
					<view class="video-info">
						<view class="video-title text-cut-2">{{ item.name }}</view>
						<view class="video-desc text-cut">{{ item.description || '暂无描述' }}</view>

						<view class="video-meta">
							<view class="video-price-container">
								<view class="video-price text-red text-bold"
									:class="{ 'price-free': item.price === 0 }">
									{{ item.price === 0 ? '免费' : '¥' + item.price }}
								</view>
								<view class="video-original-price text-gray text-del margin-left-xs"
									v-if="item.market_price > 0">
									¥{{ item.market_price }}
								</view>
							</view>
							<view class="video-views text-blue">
								<text class="cuIcon-peoplefill text-blue"></text>
								<text class="text-blue">{{item.sale_num}}人购买</text>
							</view>
						</view>
					</view>
				</view>

				<!-- 空状态 -->
				<empty v-if="isLoad == true && videoList.length == 0"></empty>

			</view>

			<!-- 底部安全区域 -->
			<view class="safe-area-bottom"></view>
		</view>
	</view>
</template>

<script>
	import {
		get
	} from "@/common/js/http.js";
	let app = getApp(),
		that = null,
		cache = app.globalData.config.storage;
	export default {
		data() {
			return {
				isLoad: false,
				keyword: '',
				videoList: [],
				appIsAudit: false,
				isLoading: false,
				hasMore: true,
				page: 1,
				pageSize: 10,
				timer: null
			}
		},
		onLoad() {
			app.globalData.showShareMenu();
			that = this;
		},
		onShow() {
			that.appIsAudit = app.globalData.checkAppIsAudit();
			if (that.appIsAudit) {
				uni.reLaunch({
					url: '/pages/index/index'
				});
			}
			that.getList();
		},
		onReachBottom() {
			if (that.hasMore && !that.isLoading) {
				that.page++;
				that.getList();
			}
		},
		methods: {
			async getList() {
				if (!that.hasMore) {
					return
				}
				that.isLoading = true;
				var res = await get('videoCollection/getList', {
					page: that.page,
					pageSize: that.pageSize,
					keyword: that.keyword
				})

				// 如果是第一页，直接赋值；如果是加载更多，则追加数据
				if (that.page === 1) {
					that.videoList = res.data || [];
				} else {
					that.videoList = [...that.videoList, ...(res.data || [])];
				}

				that.isLoad = true;
				that.isLoading = false; // 重置加载状态
				if (res.data.length < that.pageSize) {
					that.hasMore = false;
				}
			},

			// 搜索输入变化
			onInputChange(e) {

			},

			// 搜索点击
			searchTap() {
				// 重置页码并执行搜索
				that.page = 1;
				that.videoList = [];
				that.hasMore = true;
				that.getList();
			},

			// 视频点击
			videoTap(e) {
				const index = e.currentTarget.dataset.index;
				const video = this.videoList[index];
				console.log('点击视频:', video);
				// 跳转到视频详情页
				uni.navigateTo({
					url: `/pages/practice/vip/detail?id=${video.id}`
				});
			},
		}
	}
</script>

<style lang="scss">
	/* 视频列表 */
	.video-list {
		padding-top: 20rpx;
		display: flex;
		flex-direction: column;
		gap: 30rpx;
	}

	/* 视频卡片 */
	.video-card {
		background-color: #fff;
		border-radius: 12rpx;
		overflow: hidden;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);

		.video-cover {
			position: relative;
			width: 100%;
			height: 360rpx;

			image {
				width: 100%;
				height: 100%;
			}

			.video-duration {
				position: absolute;
				bottom: 16rpx;
				right: 16rpx;
				background-color: rgba(0, 0, 0, 0.6);
				color: #fff;
				font-size: 24rpx;
				padding: 4rpx 12rpx;
				border-radius: 6rpx;
			}

			.video-tag {
				position: absolute;
				top: 16rpx;
				left: 16rpx;
				background-color: #FF6A00;
				color: #fff;
				font-size: 24rpx;
				padding: 4rpx 12rpx;
				border-radius: 6rpx;
			}

			.play-icon {
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);
				width: 100rpx;
				height: 100rpx;
				border-radius: 50%;
				background-color: rgba(0, 0, 0, 0.5);
				display: flex;
				justify-content: center;
				align-items: center;

				.cuIcon-playfill {
					font-size: 50rpx;
					color: #fff;
					margin-left: 8rpx;
					/* 稍微偏右一点，视觉上更居中 */
				}
			}
		}

		.video-info {
			padding: 20rpx;

			.video-title {
				font-size: 32rpx;
				font-weight: bold;
				line-height: 1.4;
				margin-bottom: 10rpx;
			}

			.video-desc {
				font-size: 26rpx;
				color: #666;
				line-height: 1.4;
				margin-bottom: 16rpx;
			}

			.video-meta {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.video-price-container {
					display: flex;
					align-items: center;
				}

				.video-price {
					font-size: 30rpx;
					color: #FF0000;
					font-weight: bold;

					&.price-free {
						color: #1890FF;
					}
				}

				.video-original-price {
					font-size: 24rpx;
					color: #999;
					text-decoration: line-through;
				}

				.video-views {
					font-size: 24rpx;
					color: #999;
					display: flex;
					align-items: center;
					gap: 6rpx;
				}
			}
		}
	}

	/* 空状态 */
	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 0;

		.empty-icon {
			width: 200rpx;
			height: 200rpx;
			margin-bottom: 30rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #f0f7ff;
			border-radius: 50%;
			box-shadow: 0 10rpx 20rpx rgba(0, 0, 0, 0.05);

			.cuIcon-videofill {
				font-size: 100rpx;
				color: #1890FF;
			}
		}

		text {
			font-size: 30rpx;
			color: #666;
			margin-top: 10rpx;
		}
	}



	/* 底部安全区域 */
	.safe-area-bottom {
		height: env(safe-area-inset-bottom);
	}

	/* 文本截断 */
	.text-cut-2 {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}
</style>